package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imufe.chemicalenterprisedangerwarning.entity.po.CheckItemPO;
import com.imufe.chemicalenterprisedangerwarning.entity.po.ProjectPO;
import com.imufe.chemicalenterprisedangerwarning.entity.query.CheckItemQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.CheckItemVO;
import com.imufe.chemicalenterprisedangerwarning.exception.ValidationException;
import com.imufe.chemicalenterprisedangerwarning.mapper.CheckItemMapper;
import com.imufe.chemicalenterprisedangerwarning.service.CheckItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import com.imufe.chemicalenterprisedangerwarning.util.ListUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Max;
import java.util.List;

/**
 * <p>
 * 检查条目表 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-10-05
 */
@Log4j2
@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItemPO> implements CheckItemService {

    @Autowired
    CheckItemMapper mapper;

    @Override
    public CheckItemVO detailed(Long id) throws ValidationException {
        CheckItemPO po = mapper.selectById(id);
        CheckItemVO vo = BeanUtils.copyPOJO(po, CheckItemVO.class);
        String chineseValue = BeanUtils.digitalToChinese(po.getEntryNumber());
        vo.setFullName(String.format("《%s》%s", po.getSource(), chineseValue));
        return vo;
    }

    @Override
    public IPage<CheckItemVO> page(Long current, String source) {
        Page<CheckItemPO> poPage = new Page<>(current, 10);
        QueryWrapper<CheckItemPO> qw = new QueryWrapper<>();
        qw.select("id");
        if (source != null) qw.eq("source", source);
        mapper.selectPage(poPage, qw);

        // po 转 vo
        Page<CheckItemVO> voPage = ListUtils.copyPage(poPage, CheckItemVO.class);
        List<CheckItemPO> poRecords = poPage.getRecords();
        List<CheckItemVO> checkItemVOS = poRecords.stream().map(po -> {
            try {
                return this.detailed(po.getId());
            } catch (ValidationException e) {
                throw new RuntimeException(e);
            }

        }).toList();
        voPage.setRecords(checkItemVOS);
        return voPage;
    }

    @Override
    public List<String> source() {

        QueryWrapper<CheckItemPO> qw = new QueryWrapper<>();
        // 去重查询源字段
        qw.select("DISTINCT source");
        List<CheckItemPO> polist = mapper.selectList(qw);
        return polist.stream().map(CheckItemPO::getSource).toList();
    }

    @Override
    public CheckItemVO create(CheckItemQuery query) throws ValidationException {
        CheckItemPO po = BeanUtils.copyPOJO(query, CheckItemPO.class);
        log.info(po);
        mapper.insert(po);
        return this.detailed(po.getId());
    }

    @Override
    public CheckItemVO update(CheckItemQuery query) throws ValidationException {
        CheckItemPO po = BeanUtils.copyPOJO(query, CheckItemPO.class);
        log.info(po);
        mapper.insert(po);
        return this.detailed(po.getId());
    }
}
